package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.ZnDistrict;
import com.example.zhunongzonghe.entity.ZnScenicspot;
import com.example.zhunongzonghe.service.IZnDistrictService;
import com.example.zhunongzonghe.service.IZnScenicspotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/znScenicspot")
public class ZnScenicspotController {
    @Autowired
    private IZnScenicspotService znScenicspotService;

    @Autowired
    private IZnDistrictService iZnDistrictService;

    /**
     * 后台分页查询全部，分页搜索
     * @param pageId
     * @param pageNum
     * @param Search
     * @return
     */
    @GetMapping("/SearchScenicAll")
    public Result<Page> SearchScenicAll(@RequestParam("pageId") Integer pageId,
                                        @RequestParam("pageSize") Integer pageNum,
                                        @RequestParam("input") Object Search){
        Page<ZnScenicspot> znScenicspotBeanPage = znScenicspotService.SearchAllSpot(pageId,pageNum,Search);
        return new Result<>(true,"查询成功",200,znScenicspotBeanPage);
    }

    @GetMapping("/SearchDisAll")
    public Result<List<ZnDistrict>> SearchDisAll(){
        List<ZnDistrict> znDistrictList = iZnDistrictService.list();
        return new Result<>(true,"查询成功",200,znDistrictList);
    }
    @PutMapping("/editorScenic")
    public Result<ZnScenicspot> editorScenic(@RequestBody ZnScenicspot znScenicspot){
        znScenicspot.setUpdateTime(LocalDateTime.now());
        znScenicspotService.updateById(znScenicspot);
        return new Result<>(true,"编辑成功",200);
    }

    /**
     * 后台新增景区
     * @param znScenicspot
     * @return
     */
    @PostMapping("/AddScenic")
    public Result<ZnDistrict> AddScenic(@RequestBody ZnScenicspot znScenicspot){
        if(znScenicspot!=null){
            znScenicspot.setCreateTime(LocalDateTime.now());
            znScenicspotService.save(znScenicspot);
            return new Result<>(true,"添加成功",200);
        }else {
            return new Result<>(false,"失败",400);
        }
    }

    @DeleteMapping("/deleteScenic/{spotId}")
    public Result<ZnScenicspot> deleteScenic(@PathVariable Integer spotId){
        znScenicspotService.removeById(spotId);
        return new Result<>(true,"删除成功",200);
    }

    @GetMapping("/SearHomeSpot")
    public Result<List<ZnScenicspot>> SearHomeSpot(){
        List<ZnScenicspot> znScenicspotList = znScenicspotService.list();
        return new Result<>(true,"查询成功",200,znScenicspotList);
    }

    /**
     * 查询单个景点信息
     * @param spotId
     * @return
     */
    @GetMapping("/searchScenicInfo/{spotId}")
    public Result<ZnScenicspot> searchScenicInfo(@PathVariable Integer spotId){
        ZnScenicspot znScenicspot = znScenicspotService.SearchScenicInfo(spotId);
        return new Result<>(true,"查询成功",200,znScenicspot);
    }

    /**
     * 热门
     * @return
     */
    @GetMapping("/hotScenicSpot")
    public Result<List<ZnScenicspot>> hotsScenic(){
        List<ZnScenicspot> list = znScenicspotService.list();
        return new Result<>(true,"查询成功",200,list);
    }
    @GetMapping("/ScenicAll")
    public Result<Page> SearchScenicAll(@RequestParam("pageId") Integer pageId,
                                        @RequestParam("pageSize") Integer pageNum
                                        ){
        Page<ZnScenicspot> page = new Page<>(pageId,pageNum);
        Page<ZnScenicspot> znScenicspotPage = znScenicspotService.page(page);
        return new Result<>(true,"查询成功",200,znScenicspotPage);
    }
}